package com.saturn.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.saturn.app.db.DymaticCondition;
import com.saturn.app.db.ListData;
import com.saturn.app.db.ORMapping;
import com.saturn.app.db.ResultORMapping;
import com.saturn.app.db.SimpleDaoTemplate;
import com.saturn.app.exception.BSException;

public class Dict {

	private String id;
	private String value;
	private String type;
	private String pinyin;

	private static ORMapping<Dict> mapping = new ResultORMapping<Dict>();

	public static int add(Dict dict) {
		if (get(dict.getId()) != null) {
			throw new BSException("数据字典ID重复,id=" + dict.getId());
		}

		return SimpleDaoTemplate.update(
				"INSERT INTO app_dict VALUES(?, ?, ?, ?)", dict.getId(),
				dict.getValue(), dict.getType(), dict.getPinyin());
	}

	public static Dict get(String id) {
		return SimpleDaoTemplate.queryOne("SELECT * FROM app_dict WHERE 1 = 1",
				new DymaticCondition().addCondition("and id = '?'", id),
				mapping, Dict.class);
	}

	public static int edit(Dict dict) {
		return SimpleDaoTemplate
				.update("UPDATE app_dict SET value = ?, type = ?, pinyin = ? WHERE id = ?",
						dict.getValue(), dict.getType(), dict.getPinyin(),
						dict.getId());
	}

	public static ListData<Dict> getDicts(Dict dict, String start,
			String offset, String orderBy, String order) {

		DymaticCondition addCondition = new DymaticCondition()
				.addSimpleCondition(dict, "id", "value", "type", "pinyin");

		int total = SimpleDaoTemplate.queryCount(
				"SELECT count(*) FROM app_dict WHERE 1 = 1", addCondition);

		List<Dict> list = SimpleDaoTemplate.query(
				"SELECT * FROM app_dict WHERE 1 = 1", addCondition
						.addCondition("ORDER BY {0} {1}", orderBy, order)
						.addCondition("LIMIT {0}, {1}", start, offset),
				mapping, Dict.class);

		return new ListData<Dict>(total, list);
	}

	public static int remove(final String id) {
		return SimpleDaoTemplate
				.update("DELETE FROM app_dict WHERE id = ?", id);
	}

	public static void removes(String[] ids) {
		if (ids != null) {
			for (String id : ids) {
				remove(id);
			}
		}
	}
	
	public static Map<String, Map<String, String>> getDicts() {
		Map<String, Map<String, String>> dicts = new HashMap<String, Map<String, String>>();
		List<Map<String, String>> dictTypes = SimpleDaoTemplate.query(
				"select distinct type from app_dict", null, "type");

		for (Map<String, String> dict : dictTypes) {
			dicts.put(dict.get("type"), queryDictByTypes(dict.get("type")));
		}
		return dicts;
	}

	private static Map<String, String> queryDictByTypes(String type) {
		Map<String, String> map = new HashMap<String, String>();
		List<Dict> dictTypes = SimpleDaoTemplate.query(
				"select * from app_dict where 1=1", new DymaticCondition()
						.addCondition(" and type = '?'", type), mapping,
				Dict.class);
		for (Dict dict: dictTypes) {
			map.put(dict.getValue(), dict.getPinyin());
		}
		return map;
	}

	public Dict() {
	}

	public Dict(String id, String value, String type, String pinyin) {
		super();
		this.id = id;
		this.value = value;
		this.type = type;
		this.pinyin = pinyin;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPinyin() {
		return pinyin;
	}

	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

	@Override
	public String toString() {
		return new JSONObject(this).toString();
	}
}
